/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MensagemGUI.java
 *
 * Created on 21/04/2011, 14:23:03
 */
package br.com.fritadorcorrente.telas;

import br.com.fritadorcorrente.objetos.Mensagem;
import br.com.fritadorcorrente.objetos.dao.GenericoDAO;
import br.com.fritadorcorrente.utils.CamadaBase;
import br.com.fritadorcorrente.utils.spring.Contexto;
import java.io.Serializable;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author manchini
 */
public class MensagemGUI extends javax.swing.JDialog {
    
    private Mensagem mensagem;

    /** Creates new form MensagemGUI */
    public MensagemGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        jTable1.getColumnModel().removeColumn(jTable1.getColumn("Objeto"));
        
        atualizarTabela();
    }
    
    
    private void atualizarTabela(){
        List<Serializable> listarTodos = Contexto.getContext().getBean(GenericoDAO.class).listarTodos(Mensagem.class);
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setNumRows(0);
        for (int i = 0; i < listarTodos.size(); i++) {
            Mensagem object = (Mensagem) listarTodos.get(i);
            Object[] row = new Object[3];
            row[0] = object.getCod();
            row[1] = object.getMensagem();
            row[2] = object;
            model.addRow(row);
            
        }
    }
    
    
    private void salvar(){
        if(mensagem==null)
            mensagem = Contexto.getContext().getBean(Mensagem.class);
           
        mensagem.setMensagem(jTextArea1.getText());
        Contexto.getContext().getBean(GenericoDAO.class).gravar(mensagem);
        atualizarTabela();
    }
    
    
    private void excluir(){    
        
        Contexto.getContext().getBean(GenericoDAO.class).excluir(Mensagem.class,new Integer(jTextField1.getText()));
        atualizarTabela();
    }
    
    private void limpar(){
        mensagem = null;
        jTextArea1.setText("");
        jTextField1.setText("");
        
    }
    
    private void selecionar(){
        mensagem = (Mensagem)jTable1.getModel().getValueAt(jTable1.getSelectedRow(), jTable1.getColumnCount());
        jTextField1.setText(mensagem.getCod().toString());
        jTextArea1.setText(mensagem.getMensagem());
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton_Delete = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Cod:");
        jLabel1.setOpaque(true);
        jLabel1.setPreferredSize(new java.awt.Dimension(80, 18));
        jPanel3.add(jLabel1);

        jTextField1.setFocusable(false);
        jTextField1.setPreferredSize(new java.awt.Dimension(100, 30));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jPanel3.add(jTextField1);

        jPanel1.add(jPanel3);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jLabel2.setText("Mensagem:");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel2.setPreferredSize(new java.awt.Dimension(90, 18));
        jLabel2.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        jPanel4.add(jLabel2, java.awt.BorderLayout.WEST);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jPanel4.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel4);

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton1);

        jButton_Delete.setText("Delete");
        jButton_Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_DeleteActionPerformed(evt);
            }
        });
        jPanel5.add(jButton_Delete);

        jButton3.setText("Limpar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton3);

        jPanel1.add(jPanel5);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Mensagens"));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod", "Mensagem", "Objeto"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-471)/2, (screenSize.height-437)/2, 471, 437);
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       salvar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton_DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_DeleteActionPerformed
      excluir();
    }//GEN-LAST:event_jButton_DeleteActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       limpar();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            selecionar();
        }
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        CamadaBase.start();
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MensagemGUI dialog = new MensagemGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        CamadaBase.stop();
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton_Delete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
